package com.ppc.controller.common;

import com.ppc.constant.JwtClaimsConstant;
import com.ppc.constant.UserConstant;
import com.ppc.dto.RegisterDTO;
import com.ppc.properties.JwtProperties;
import com.ppc.result.Result;
import com.ppc.service.CommonService;
import com.ppc.utils.JwtUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


/**
 * 通用接口
 *
 * @author jianKangS1
 * @date 2023/12/20
 */
@RestController
@RequestMapping("/common")
@Api(tags = "通用接口")
@Slf4j
public class CommonController {

  @Autowired
  private CommonService commonService;

  @Autowired
  private JwtProperties jwtProperties;

  @Autowired
  private RedisTemplate<String, String> redisTemplate;

  /**
   * 验证邮箱
   *
   * @param email
   * @return {@link Result }
   * @description
   */
  @PostMapping("/register/verify-email")
  @ApiOperation("验证邮箱")
  public Result<Map<String,String>> verifyEmail(@RequestParam(value = "email") String email) {
    log.info("验证邮箱，{}", email);
    String verifyCode = commonService.verifyEmail(email);
    redisTemplate.opsForValue().set(email, verifyCode, 3, TimeUnit.MINUTES);
    Map<String,String> emailMap=new HashMap();
    emailMap.put("userEmail",email);
    emailMap.put("userVerifyCode",verifyCode);
    return Result.success(emailMap);
  }

  /**
   * 验证手机号
   *
   * @param phone
   * @return {@link Result }
   * @description
   */
  @PostMapping("/register/verify-phone")
  @ApiOperation("验证手机号")
  public Result<Map<String,String>> verifyPhone(@RequestParam(value = "phone") String phone) {
    log.info("验证手机号，{}", phone);
    String verifyCode = commonService.verifyPhone(phone);
    redisTemplate.opsForValue().set(phone, verifyCode, 3, TimeUnit.MINUTES);
    Map<String,String> emailMap=new HashMap();
    emailMap.put("userPhone",phone);
    emailMap.put("userVerifyCode",verifyCode);
    return Result.success(emailMap);
  }

  /**
   * 注册工人或雇主
   *
   * @param registerDTO
   * @return {@link Result }<{@link String }>
   * @description
   */
  @PostMapping("/register")
  @ApiOperation("注册工人或雇主")
  public Result<String> register(@RequestBody RegisterDTO registerDTO) {
    log.info("注册传递信息，{}", registerDTO);
    String verifycode = null;
    if (registerDTO.getEmail() != null && registerDTO.getEmail() != "") {
      verifycode = (String) redisTemplate.opsForValue().get(registerDTO.getEmail());
    } else if (registerDTO.getPhone() != null && registerDTO.getPhone() != "") {
      verifycode = (String) redisTemplate.opsForValue().get(registerDTO.getPhone());
    }

    if (verifycode == null || !verifycode.equals(registerDTO.getVerifyCode())) {
      return Result.error("验证码不正确");
    }
    Long userID = commonService.register(registerDTO);

    //登录成功后，生成jwt令牌
    Map<String, Object> claims = new HashMap<>();

    String token = "";
    if (registerDTO.getUserType() == UserConstant.EMPLOYER) {
      claims.put(JwtClaimsConstant.EMPLOYER_ID, userID);
      token = JwtUtil.createJWT(
          jwtProperties.getEmployerSecretKey(),
          jwtProperties.getEmployerTtl(),
          claims);

    } else if (registerDTO.getUserType() == UserConstant.WORKER) {
      claims.put(JwtClaimsConstant.WORKER_ID, userID);
      token = JwtUtil.createJWT(
          jwtProperties.getWorkerSecretKey(),
          jwtProperties.getWorkerTtl(),
          claims);
    } else {
      return Result.error("unknown usertype ");
    }

    return Result.success(token);
  }

  @PostMapping("/check_phone_exist")
  @ApiOperation("电话是否被注册")
  public Result<Boolean> checkPhoneRegister(@RequestParam(value = "phone") String phone){
    Boolean result=commonService.checkUserExist(phone,null);
    return Result.success(result);
  }
  @PostMapping("/check_email_exist")
  @ApiOperation("邮箱是否被注册")
  public Result<Boolean> checkEmailRegister(@RequestParam(value = "email") String email){
    Boolean result=commonService.checkUserExist(null,email);
    return Result.success(result);
  }
}
